//
//  UtilObject.m
//  WangHuiJinRong
//
//  Created by pro on 15/1/24.
//  Copyright (c) 2015年 158954945. All rights reserved.
//

#import "UtilObject.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import "IPAddress.h"

#import "NoDataTipView.h"



@implementation UtilObject





+(CGSize)returnTextSize:(NSString *)text and:(CGFloat)width andF:(UIFont *)font
{
    return  [self returnTextSize:text and:width andF:font andP:0];
}


+(CGSize)returnTextSize:(NSString *)text and:(CGFloat)width andF:(UIFont *)font andP:(CGFloat)par
{
    if (IsiOS7Later) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        paragraphStyle.lineSpacing = par;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
        return    [text boundingRectWithSize:Size(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    }else{
        return   [text sizeWithFont:font constrainedToSize:Size(width, CGFLOAT_MAX)];
    }
    
}

+(UIStoryboard *)mainStoryboard
{
    return [UIStoryboard storyboardWithName:@"Main" bundle:nil];
}


+(id)webVC
{
    UIStoryboard *sb =  [self mainStoryboard];
    return [sb instantiateViewControllerWithIdentifier:@"WebVC"];
}

#pragma mark -  check phone Num
+(BOOL)checkTel:(NSString *)str
{
    if (!str || [str isEqualToString:@""]) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入手机号码" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    }
    str =  [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (str.length != 11) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号码必须为11位！" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    }
    
//    NSString *regex = @"^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
//    
//    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
//    
//    BOOL isMatch = [pred evaluateWithObject:str];
//    
//    if (!isMatch) {
//        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        [alert show];
//        
//        return NO;
//    }
    return YES;
    
}


+(BOOL)checkStr:(NSString *)str
{
    if (str) {
       str =  [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if (![str isEqualToString:@""]) {
            return YES;
        }
    }
    return NO;
}
+(BOOL)checkCarStr:(NSString *)str
{
    if (str) {
        str =  [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if (![str isEqualToString:@""] && str.length==7) {
            return YES;
        }
    }
    return NO;
}
+(BOOL)checkCardStr:(NSString *)str
{
    if (str) {
        str =  [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if (![str isEqualToString:@""] && str.length==18) {
            return YES;
        }
    }
    return NO;
}

+(id)getNodataTipView
{
    return [[NSBundle mainBundle]loadNibNamed:@"NoDataView" owner:nil options:nil][0];
    
    
//    UIView *backView = [[NoDataTipView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, ScreenHeight)];
    
//    return backView;
    
}
+(id)getNoViolationDataView
{
    UIView *backView = [[NSBundle mainBundle]loadNibNamed:@"NoViolationDataView" owner:nil options:nil][0];
    
    CGRect backViewFrame = backView.frame;
    
    backViewFrame.size.width = ScreenWidth;
    
    [backView setFrame:backViewFrame];
    
    return backView;
}

+(id)getPushListData
{
    
    NSArray *pushList= [NSArray arrayWithContentsOfFile:[self pushListPath]];
    if (!pushList) {
        pushList = [NSArray new];
    }
    return pushList;
}

+(void)savePushList:(id)list
{
    NSAssert(list, @"列表为空");
    NSAssert([list isKindOfClass:[NSArray class]], @"数据不是数组");
    BOOL work = [list writeToFile:[self pushListPath] atomically:YES];
    NSAssert(work, @"保存数据失败");
}

+(NSString *)pushListPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    return [cachesDir stringByAppendingPathComponent:@"pushList.plist"];
}


+(NSString *)timeForSec:(double)secStr andFormatterStr:(NSString *)formatterStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formatterStr];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:secStr/1000.0];
    NSString *timeStr = [formatter stringFromDate:date];    
    return timeStr;
}

+(NSString *)time:(NSDate *)date andFormatterStr:(NSString *)formatterStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formatterStr];
    NSString *timeStr = [formatter stringFromDate:date];
    return timeStr;
}


+(BOOL)compareNewVersion:(NSString *)ver0 andCurrent:(NSString *)ver1
{
    NSArray *arr0  = [ver0 componentsSeparatedByString:@"."];
    NSArray *arr1  = [ver1 componentsSeparatedByString:@"."];
    NSInteger length = MAX(arr0.count, arr1.count);
    for (int i = 0; i < length; i ++) {
        NSInteger number0,number1;
        if (arr0.count>i) {
            number0 = [arr0[i] integerValue];
        }else
        {
            number0 = 0;
        }
        if (arr1.count>i) {
            number1 = [arr1[i] integerValue];
        }else
        {
            number1 = 0;
        }
        if (number0 > number1) {
            return YES;
        }
    }
    return NO;
}


+(void)log
{
    CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
    NSLog(@"%@", networkInfo.currentRadioAccessTechnology);
}

+(NSString *)toString:(id)obj
{
    if ([obj isKindOfClass:[NSNull class]]) {
        return @"";
    }else
    {
        return [NSString stringWithFormat:@"%@",obj];
    }
}


+(NSString *)iPAddress
{
        InitAddresses();
        GetIPAddresses();
        GetHWAddresses();
    return [NSString stringWithFormat:@"%s", ip_names[1]];
}


+(NSString *)netType
{
    AFNetworkReachabilityStatus status = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
    switch (status) {
        case AFNetworkReachabilityStatusUnknown:
            return @"未知";
            break;
        case AFNetworkReachabilityStatusNotReachable:
            return @"无网络";
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            return @"2G";
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            return @"Wifi";
            break;
        default:
            break;
    }
}
@end
